package com.rbac.admin.common.result;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;

/**
 * 通用返回结果
 * 
 * @author rbac-admin
 * @since 2024-01-20
 */
@Data
@Schema(description = "通用返回结果")
public class Result<T> implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @Schema(description = "状态码")
    private Integer code;
    
    @Schema(description = "返回消息")
    private String message;
    
    @Schema(description = "返回数据")
    private T data;
    
    @Schema(description = "时间戳")
    private Long timestamp;
    
    public Result() {
        this.timestamp = System.currentTimeMillis();
    }
    
    public Result(Integer code, String message) {
        this();
        this.code = code;
        this.message = message;
    }
    
    public Result(Integer code, String message, T data) {
        this();
        this.code = code;
        this.message = message;
        this.data = data;
    }
    
    /**
     * 成功返回结果
     */
    public static <T> Result<T> success() {
        return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }
    
    /**
     * 成功返回结果
     * 
     * @param data 返回数据
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }
    
    /**
     * 成功返回结果
     * 
     * @param message 返回消息
     * @param data 返回数据
     */
    public static <T> Result<T> success(String message, T data) {
        return new Result<>(ResultCode.SUCCESS.getCode(), message, data);
    }
    
    /**
     * 失败返回结果
     */
    public static <T> Result<T> failed() {
        return new Result<>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }
    
    /**
     * 失败返回结果
     * 
     * @param message 返回消息
     */
    public static <T> Result<T> failed(String message) {
        return new Result<>(ResultCode.FAILED.getCode(), message);
    }
    
    /**
     * 失败返回结果
     * 
     * @param code 状态码
     * @param message 返回消息
     */
    public static <T> Result<T> failed(Integer code, String message) {
        return new Result<>(code, message);
    }
    
    /**
     * 失败返回结果
     * 
     * @param resultCode 返回结果状态码
     */
    public static <T> Result<T> failed(ResultCode resultCode) {
        return new Result<>(resultCode.getCode(), resultCode.getMessage());
    }
}